<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-10-10
 * Time: 下午2:42
 */

namespace Api\Common;

class Hxcall
 {
    
    // private $app_key = 'qqqqqq#aaaaaa';
    // private $client_id = 'sdasdasdasd';
    // private $client_secret = 'sdsdsdsdsd';
    // private $url = "https://a1.easemob.com/qqqqqq/aaaaaa";


    /**
     * 环信-服务器端REST API
     */
    // private $app_key = 'p791600444#xiuliqiankun';
    private $client_id = 'YXA6crSXgG9VEeaeUxXpHBPv5w';
    private $client_secret = 'YXA6HSLCodRnBdMjzFfYVhkP7bardpY';
    private $url = 'https://a1.easemob.com/p791600444/xiuliqiankun';
    /*
     * 获取APP管理员Token
     */
    function __construct()
    {
        // $url = $this->url . "/token";
        // $data = array(
        //     'grant_type' => 'client_credentials',
        //     'client_id' => $this->client_id,
        //     'client_secret' => $this->client_secret
        // );
        // $rs = json_decode($this->curl($url, $data), true);
        // $this->token = $rs['access_token'];

        $this->token = $this->getToken();
    }

    /*
     * 注册IM用户(授权注册)
     */
    public function hx_register($username, $password, $nickname)
    {
        $url = $this->url . "/users";
        $data = array(
            'username' => $username,
            'password' => $password,
            'nickname' => $nickname
        );
        $header = array(
            'Content-Type: application/json',
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, $data, $header, "POST");
    }
    /*
     * 给IM用户的添加好友
     */
    public function hx_contacts($owner_username, $friend_username)
    {
        $url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, "", $header, "POST");
    }
    /*
     * 解除IM用户的好友关系
     */
    public function hx_contacts_delete($owner_username, $friend_username)
    {
        $url = $this->url . "/users/${owner_username}/contacts/users/${friend_username}";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, "", $header, "DELETE");
    }
    /*
     * 查看好友
     */
    public function hx_contacts_user($owner_username)
    {
        $url = $this->url . "/users/${owner_username}/contacts/users";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, "", $header, "GET");
    }
    
    /* 发送文本消息 */
    public function hx_send($sender, $receiver, $msg)
    {
        $url = $this->url . "/messages";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        $data = array(
            'target_type' => 'users',
            'target' => array(
                '0' => $receiver
            ),
            'msg' => array(
                'type' => "txt",
                'msg' => $msg
            ),
            'from' => $sender,
            'ext' => array(
                'attr1' => 'v1',
                'attr2' => "v2"
            )
        );
        return $this->curl($url, $data, $header, "POST");
    }
    /* 查询离线消息数 获取一个IM用户的离线消息数 */
    public function hx_msg_count($owner_username)
    {
        $url = $this->url . "/users/${owner_username}/offline_msg_count";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, "", $header, "GET");
    }
    
    /*
     * 获取IM用户[单个]
     */
    public function hx_user_info($username)
    {
        $url = $this->url . "/users/${username}";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, "", $header, "GET");
    }
    /*
     * 获取IM用户[批量]
     */
    public function hx_user_infos($limit)
    {
        $url = $this->url . "/users?${limit}";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, "", $header, "GET");
    }
    /*
     * 重置IM用户密码
     */
    public function hx_user_update_password($username, $newpassword)
    {
        $url = $this->url . "/users/${username}/password";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        $data['newpassword'] = $newpassword;
        return $this->curl($url, $data, $header, "PUT");
    }
    
    /*
     * 删除IM用户[单个]
     */
    public function hx_user_delete($username)
    {
        $url = $this->url . "/users/${username}";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        return $this->curl($url, "", $header, "DELETE");
    }
    /*
     * 修改用户昵称
     */
    public function hx_user_update_nickname($username, $nickname)
    {
        $url = $this->url . "/users/${username}";
        $header = array(
            'Authorization: Bearer ' . $this->token
        );
        $data['nickname'] = $nickname;
        return $this->curl($url, $data, $header, "PUT");
    }
    /*
     *
     * curl
     */
    private function curl($url, $data, $header = false, $method = "POST")
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $ret = curl_exec($ch);
        return $ret;
    }

/**
     * 获取Token
     */
    public function getToken() {
        $option['grant_type'] = "client_credentials";
        $option['client_id'] = $this->client_id;
        $option['client_secret'] = $this->client_secret;
        $url = $this->url . "/token";
        $fp = @fopen( APP_PATH."easemob.txt", 'r' );
        // if($fp) {
        //     $arr = unserialize( fgets( $fp ) );
        //     if($arr['expires_in'] < time()) {
        //         $result = $this->curl( $url, $option);
        //         $result['expires_in'] = $result['expires_in'] + time();
        //         file_put_contents(APP_PATH."easemob.txt", serialize($result));
        //         return $result['access_token'];
        //         fclose( $fp );
        //         exit();
        //     }
        //     return $arr['access_token'];
        //     fclose( $fp );
        //     exit();
        // }
        $result = json_decode($this->curl( $url, $option), true);
        $result['expires_in'] = $result['expires_in'] + time();
        file_put_contents(APP_PATH."easemob.txt", serialize($result));
        return $result['access_token'];
        // fclose( $fp );
    }

 }